package com.android.settings.ios.view;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.provider.Settings;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreference;

import com.android.settings.ios.R;

public class OsSwitchPreference extends SwitchPreference implements IOsPreference {

    private int mOutline;

    private String mValueOn;
    private String mValueOff;
    private String mGlobalKey;
    private String mSystemKey;
    private String mSecureKey;
    private String mSecondTextStr;
    private int mSecondTextColor;
    private ContentResolver mCr;

    public OsSwitchPreference(Context context) {
        super(context);
    }

    public OsSwitchPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        initOsAttributeSet(context, attrs, 0);
    }

    public OsSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initOsAttributeSet(context, attrs, defStyleAttr);
    }

    public OsSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initOsAttributeSet(context, attrs, defStyleAttr);
    }

    private void initOsAttributeSet(Context context, AttributeSet attrs, int defStyleAttr) {
        mCr = context.getContentResolver();
        if (attrs != null) {
            TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.OsSwitchPreference, defStyleAttr, 0);
            mValueOn = a.getString(R.styleable.OsSwitchPreference_osOnValue);
            mValueOff = a.getString(R.styleable.OsSwitchPreference_osOffValue);
            mGlobalKey = a.getString(R.styleable.OsSwitchPreference_osSettingGlobalKey);
            mSystemKey = a.getString(R.styleable.OsSwitchPreference_osSettingSystemKey);
            mSecureKey = a.getString(R.styleable.OsSwitchPreference_osSettingSecureKey);
            TypedArray oa = context.getTheme().obtainStyledAttributes(attrs, R.styleable.OsPreference, defStyleAttr, 0);
            mSecondTextStr = oa.getString(R.styleable.OsPreference_secondaryText);
            mSecondTextColor = oa.getColor(R.styleable.OsPreference_secondaryTextColor, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                a.close();
            }
        }
    }

    @Override
    public void onAttached() {
        super.onAttached();
        setIconSpaceReserved(false);
        setLayoutResource(R.layout.os_preference_switch);

        if (mGlobalKey != null) {
            setOnPreferenceChangeListener(mGlobalChangeListener);
            String val = Settings.Global.getString(mCr, mGlobalKey);
            if (mValueOn == null) {
                setChecked("1".equals(val));
            } else {
                setChecked(val != null && val.equals(mValueOn));
            }
        } else if (mSystemKey != null) {
            setOnPreferenceChangeListener(mSystemChangeListener);
            String val = Settings.System.getString(mCr, mSystemKey);
            if (mValueOn == null) {
                setChecked("1".equals(val));
            } else {
                setChecked(val != null && val.equals(mValueOn));
            }
        } else if (mSecureKey != null) {
            setOnPreferenceChangeListener(mSecureChangeListener);
            String val = Settings.Secure.getString(mCr, mSecureKey);
            if (mValueOn == null) {
                setChecked("1".equals(val));
            } else {
                setChecked(val != null && val.equals(mValueOn));
            }
        }
    }

    private final OnPreferenceChangeListener mGlobalChangeListener = new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (mGlobalKey != null) {
                if (newValue instanceof Boolean) {
                    if ((Boolean) newValue) {
                        Settings.Global.putString(mCr, mGlobalKey, mValueOn == null ? "1" : mValueOn);
                    } else {
                        Settings.Global.putString(mCr, mGlobalKey, mValueOff == null ? "0" : mValueOff);
                    }
                }
            }
            return true;
        }
    };

    private final OnPreferenceChangeListener mSystemChangeListener = new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (mSystemKey != null) {
                if (newValue instanceof Boolean) {
                    if ((Boolean) newValue) {
                        Settings.System.putString(mCr, mSystemKey, mValueOn == null ? "1" : mValueOn);
                    } else {
                        Settings.System.putString(mCr, mSystemKey, mValueOff == null ? "0" : mValueOff);
                    }
                }
            }
            return true;
        }
    };

    private final OnPreferenceChangeListener mSecureChangeListener = new OnPreferenceChangeListener() {

        @Override
        public boolean onPreferenceChange(Preference preference, Object newValue) {
            if (mSecureKey != null) {
                if (newValue instanceof Boolean) {
                    if ((Boolean) newValue) {
                        Settings.Secure.putString(mCr, mSecureKey, mValueOn == null ? "1" : mValueOn);
                    } else {
                        Settings.Secure.putString(mCr, mSecureKey, mValueOff == null ? "0" : mValueOff);
                    }
                }
            }
            return true;
        }
    };

    @Override
    public void onBindViewHolder(PreferenceViewHolder holder) {
        super.onBindViewHolder(holder);
        OsBackgroundUtil.updateBackground(mOutline, holder.itemView);

        TextView tvSecondary = holder.itemView.findViewById(android.R.id.text1);
        if (tvSecondary != null) {
            if (mSecondTextStr != null && mSecondTextStr.length() > 0) {
                tvSecondary.setText(mSecondTextStr);
                tvSecondary.setVisibility(View.VISIBLE);
                if (mSecondTextColor != 0) {
                    tvSecondary.setTextColor(mSecondTextColor);
                }
            } else {
                tvSecondary.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void setItemBackgroundMode(int outline) {
        mOutline = outline;
    }
}
